package com.qf.user.controller;

import com.qf.controller.BaseController;
import com.qf.dto.sms.SmsDTO;
import com.qf.result.R;
import com.qf.user.client.SmsClient;
import com.qf.user.dto.OneKeySignInDTO;
import com.qf.user.dto.SignInDTO;
import com.qf.user.model.User;
import com.qf.user.service.UserService;
import com.qf.util.CopyFieldUtil;
import com.qf.util.JwtUtil;
import com.qf.vo.UserVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Random;

/**
 * uri规范：一般来说分为三段{项目名/模块名/功能名}
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.04.01
 * @since 1.0
 */
@RestController
@RequestMapping("qf/user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @Resource
    private SmsClient smsClient;

    @Resource
    private Random random;

    @PostMapping("test")
    public R test(){
        //解析token
        return R.ok().setResult("你的id",userId())
                     .setResult("你的昵称",user().getNickname());
    }

    @PostMapping("signIn")
    public R signIn(@RequestBody SignInDTO signInDTO){
        String signInName=signInDTO.getSignInName();
        String password=signInDTO.getPassword();
        //登陆
        User user = userService.isLocked(signInName);
        if(user==null){
            //根据登录名，没有找到对应的用户
            return R.error(502,"登陆信息有误");
        }
        if(user.getIsLocked()==1){
            return R.error(501,"当前账号已经被锁定");
        }

        user=userService.signIn(signInName,password);
        if(user==null){
            return R.error(502,"登陆信息有误");
        }else{
            //签发token
            //user  ->  userVO
            UserVO userVO =new UserVO();
            //这样写太麻烦，有多少个字段就需要写多少遍
            //可以选择封装一个实体工具类
            //userVO.setNickname(user.getNickname());
            CopyFieldUtil.copy(user,userVO);
            //签发token
            String token = JwtUtil.generatorToken(userVO);
            return R.ok("登录成功")
                    .setResult("token",token)
                    .setResult("user",userVO);
        }

    }

    /**
     * 请求验证码
     */
    @PostMapping("sms")
    public R sms(@RequestBody SmsDTO smsDTO){
        //前段传入的是用户输入的手机号
        //我们设置模块id为1
        smsDTO.setTypeId(1);
        //生成六位的验证码
        //0-999999
        //code需要补位
        String code= random.nextInt(1000000)+"";
        int iMax=6-code.length();
        for (int i = 0; i <iMax; i++) {
            code=0+code;
        }
        //设置六位验证码
        smsDTO.setCode(code);
        //发送验证码
        return smsClient.send(smsDTO);
    }
    /**
     * 手机号一键登录
     */
    @PostMapping("oneKeySignIn")
    public R oneKeySignIn(@RequestBody OneKeySignInDTO oneKeySignInDTO){
        String phone=oneKeySignInDTO.getPhoneNo();
        String code = oneKeySignInDTO.getCode();
        //说明我是登录逻辑
        Integer type=1;
        SmsDTO smsDTO=new SmsDTO(phone,code,type);
        //校验短信
        R checkResult=smsClient.check(smsDTO);
        if (!checkResult.isOk()){
            //不成功
            return checkResult;
        }else {
            //成功
            User user=userService.oneKeySignIn(phone);
            //签发token
            UserVO userVo=new UserVO(user);
            String token = JwtUtil.generatorToken(userVo);
            return  R.ok("登录成功")
                    .setResult("token",token)
                    .setResult("user",userVo);
        }
    }


}
